﻿using System;
using System.Collections.Generic;
using System.Globalization;

namespace Motar
{
    
    /// <summary>
    /// 
    /// </summary>
    public class LocaleDictionaryFacade : LocaleResolverFacadeBase<IDictionary<string, CultureInfo>>
    {

        /// <summary>
        /// 
        /// </summary>
        private readonly string _defaultLocale;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="culture"></param>
        public LocaleDictionaryFacade(string key, CultureInfo culture) :
            base(new Dictionary<string, CultureInfo>())
        {
            Register(key, culture);

            _defaultLocale = key;
        }

        /// <summary>
        /// 
        /// </summary>
        public override ICollection<string> Locales
        {
            get { return Execute<ICollection<string>>(dictionary => dictionary.Keys); }
        }

        /// <summary>
        /// 
        /// </summary>
        public override string DefaultLocale
        {
            get { return _defaultLocale; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public override ILocaleResolverFacade Register(string key, CultureInfo culture)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }
            if (culture == null)
            {
                throw new ArgumentNullException("culture");
            }

            Execute(dictionary => dictionary.Add(key, culture));

            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public override CultureInfo Resolve(string key)
        {
            return Execute<CultureInfo>(dictionary => dictionary[key]);
        }
    }
}
